package com.cskaoyan.service.impl;

import com.cskaoyan.bean.StatUserData;
import com.cskaoyan.bean.vo.GoodsStatVo;
import com.cskaoyan.bean.vo.OrderStatVo;
import com.cskaoyan.mapper.MarketStatMapper;
import com.cskaoyan.service.StatService;
import com.cskaoyan.utils.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.List;

public class StatServiceImpl implements StatService {
    @Override
    public StatUserData user() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketStatMapper statMapper = sqlSession.getMapper(MarketStatMapper.class);
        List<StatUserData.Rows> rows = statMapper.selectStat();
        return StatUserData.data(rows);
    }

    @Override
    public OrderStatVo orderStat() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketStatMapper marketStatMapper = sqlSession.getMapper(MarketStatMapper.class);
        //此时查询得到rows
        List<OrderStatVo.Rows> rows = marketStatMapper.orderStat();


        //依次封装字段名cloumns
        ArrayList<String> columns = new ArrayList<>();
        columns.add("day");
        columns.add("orders");
        columns.add("customers");
        columns.add("amount");
        columns.add("pcr");



        //创建订单响应的Data数据
        OrderStatVo data =  OrderStatVo.builder()
                .columns(columns)
                .rows(rows)
                .build();

        sqlSession.close();
        return data;

    }

    @Override
    public GoodsStatVo goodsStat() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketStatMapper marketStatMapper = sqlSession.getMapper(MarketStatMapper.class);
        //此时查询得到rows
        List<GoodsStatVo.Rows> rows =marketStatMapper.goodsStat();


        //依次封装字段名cloumns
        ArrayList<String> columns = new ArrayList<>();
        columns.add("day");
        columns.add("orders");
        columns.add("products");
        columns.add("amount");



        //创建订单响应的Data数据
        GoodsStatVo data = GoodsStatVo .builder()
                .columns(columns)
                .rows(rows)
                .build();

        //关闭sqlsession
        sqlSession.close();

        return data;
    }

}
